/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts;

import com.myapp.struts.TablaPeriodicaBusinessDelegate;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import tpa.TablaPeriodica.Clasificacion;
import tpa.TablaPeriodica.Elemento;
import tpa.TablaPeriodica.SubClase;

/**
 *
 * @author leandro
 */
public class AgregarElementoActionForm extends org.apache.struts.action.ActionForm
{

    private Long _numeroAtomico;
    private String _nombre;
    private String _simbolo;
    private Float _pesoAtomico;
    private String _imageUrl;
    private String _infoUrl;
    private String _status;
    private List _clasificaciones;
    private HashMap<Long, Long> _selectedProperties;

    /**
     * @return
     */
    public String getNombre()
    {
        return _nombre;
    }

    /**
     * @param string
     */
    public void setNombre(String string)
    {
        _nombre = string;
    }

    public boolean isPropertySelected(Long subClaseId)
    {
        return this.getSelectedProperties().containsValue(subClaseId);
    }

    public void setSelectedProperty(Long subClaseId)
    {
        try
        {
            SubClase subclase = TablaPeriodicaBusinessDelegate.getInstance().getTablaPeriodicaSessionBean().ObtenerSubClase(subClaseId);

            this.getSelectedProperties().put(subclase.getParentClasificacion().getId(), subClaseId);
        }
        catch (Exception exception)
        {
        }
    }

    /**
     * @return the _simbolo
     */
    public String getSimbolo()
    {
        return _simbolo;
    }

    /**
     * @param simbolo the _simbolo to set
     */
    public void setSimbolo(String simbolo)
    {
        this._simbolo = simbolo;
    }

    /**
     * @return the _pesoAtomico
     */
    public Float getPesoAtomico()
    {
        return _pesoAtomico;
    }

    /**
     * @param pesoAtomico the _pesoAtomico to set
     */
    public void setPesoAtomico(Float pesoAtomico)
    {
        this._pesoAtomico = pesoAtomico;
    }

    /**
     * @return the _numeroAtomico
     */
    public Long getNumeroAtomico()
    {
        return _numeroAtomico;
    }

    /**
     * @param numeroAtomico the _numeroAtomico to set
     */
    public void setNumeroAtomico(Long numeroAtomico)
    {
        this._numeroAtomico = numeroAtomico;
    }

    /**
     * @return the _status
     */
    public String getStatus()
    {
        return _status;
    }

    /**
     * @param status the _status to set
     */
    public void setStatus(String status)
    {
        this._status = status;
    }

    /**
     * @return the _clasificaciones
     */
    public List getClasificaciones()
    {
        return _clasificaciones;
    }

    /**
     * @param clasificaciones the _clasificaciones to set
     */
    public void setClasificaciones(List clasificaciones)
    {
        this._clasificaciones = clasificaciones;
    }

    public List getSubClases(Clasificacion clasificacion)
    {
        List<SubClase> resultsSubClases = new ArrayList<SubClase>();

        if (clasificacion.getSubClases().size() > 0)
        {
            resultsSubClases.add(clasificacion.getSubClases().get(0));

            for (int subClaseIndex = 1; subClaseIndex < clasificacion.getSubClases().size(); subClaseIndex++)
            {
                SubClase currentSubclase = clasificacion.getSubClases().get(subClaseIndex);

                for (int resultSubClaseIndex = 0; resultSubClaseIndex < resultsSubClases.size(); resultSubClaseIndex++)
                {
                    SubClase currentResultSubClase = resultsSubClases.get(resultSubClaseIndex);
                    if (currentResultSubClase.getValor().compareTo(currentSubclase.getValor()) > 0)
                    {
                        resultsSubClases.add(resultSubClaseIndex, currentSubclase);
                        currentSubclase = null;
                        break;
                    }
                }

                if (currentSubclase != null)
                {
                    resultsSubClases.add(currentSubclase);
                }
            }
        }

        return resultsSubClases;
    }

    /**
     * @return the _selectedProperties
     */
    public HashMap<Long, Long> getSelectedProperties()
    {
        if (_selectedProperties == null)
        {
            _selectedProperties = new HashMap<Long, Long>();
        }
        return _selectedProperties;
    }

    /**
     * @param selectedProperties the _selectedProperties to set
     */
    public void setSelectedProperties(HashMap<Long, Long> selectedProperties)
    {
        this._selectedProperties = selectedProperties;
    }

    /**
     *
     */
    public AgregarElementoActionForm()
    {
        super();

        _pesoAtomico = 0.0f;
        _numeroAtomico = 0L;
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
    {
        ActionErrors errors = new ActionErrors();
        if (getNombre() == null || getNombre().length() <= 0)
        {
            errors.add("_nombre", new ActionMessage("error._field_required", "Nombre"));
        }

        if (getSimbolo() == null || getSimbolo().length() <= 0)
        {
            errors.add("_simbolo", new ActionMessage("error._field_required", "Símbolo"));
        }

        if (getNumeroAtomico() <= 0)
        {
            errors.add("_numeroAtomico", new ActionMessage("error._field_invalid", "Número Atómico"));
        }

        if (getPesoAtomico() <= 0.0f)
        {
            errors.add("_pesoAtomico", new ActionMessage("error._field_invalid", "Peso Atómico"));
        }
        return errors;
    }

    @Override
    public void reset(ActionMapping mapping, HttpServletRequest request)
    {
        try
        {
            this._clasificaciones = TablaPeriodicaBusinessDelegate.getInstance().getTablaPeriodicaSessionBean().ObtenerClasificaciones();

            if (request.getParameter("numeroAtomico") != null)
            {
                _numeroAtomico = Long.parseLong(request.getParameter("numeroAtomico"));
            }

            Elemento elemento = TablaPeriodicaBusinessDelegate.getInstance().getTablaPeriodicaSessionBean().ObtenerElemento(_numeroAtomico);
            if (_numeroAtomico <= 0L || elemento == null)
            {
                this._numeroAtomico = 0L;
                this._status = "";
                this._nombre = "";
                this._pesoAtomico = 0f;
                this._simbolo = "";

                /*for (Object currentClasificacion : this.getClasificaciones())
                {
                    getSelectedProperties().put(((Clasificacion) currentClasificacion).getId(), 0L);
                }*/
            }
            else
            {
                this._numeroAtomico = elemento.getNumeroAtomico();
                this._nombre = elemento.getNombre();
                this._simbolo = elemento.getSimbolo();
                this._pesoAtomico = elemento.getPesoAtomico();
                this._imageUrl = elemento.getImageUrl();
                this._infoUrl = elemento.getInfoUrl();

                for (SubClase currentProperty : elemento.getPropiedades())
                {
                    getSelectedProperties().put(currentProperty.getParentClasificacion().getId(), currentProperty.getId());
                }

            }

            Enumeration parameterNames = request.getParameterNames();
            while (parameterNames.hasMoreElements())
            {
                String selectedPropertyName = parameterNames.nextElement().toString();

                if (selectedPropertyName.startsWith("selectedProperty_"))
                {
                    String clasificacionIdString = selectedPropertyName.substring(selectedPropertyName.lastIndexOf("_") + 1, selectedPropertyName.length());
                    Long clasificacionId = Long.parseLong(clasificacionIdString);

                    if (request.getParameter(selectedPropertyName) != null && !request.getParameter(selectedPropertyName).equals(""))
                    {
                        Long propiedadId = Long.parseLong(request.getParameter(selectedPropertyName));
                        getSelectedProperties().put(clasificacionId, propiedadId);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            this._status = ex.getMessage();
        }
    }

    /**
     * @return the _imageUrl
     */
    public String getImageUrl()
    {
        return _imageUrl;
    }

    /**
     * @param imageUrl the _imageUrl to set
     */
    public void setImageUrl(String imageUrl)
    {
        this._imageUrl = imageUrl;
    }

    /**
     * @return the _infoUrl
     */
    public String getInfoUrl()
    {
        return _infoUrl;
    }

    /**
     * @param infoUrl the _infoUrl to set
     */
    public void setInfoUrl(String infoUrl)
    {
        this._infoUrl = infoUrl;
    }
}
